import { http } from "@/utils/http";

export type UserResult = {
  success: boolean;
  code: number;
  data: {
    /** 用户名 */
    username: string;
    /** 当前登陆用户的角色 */
    roles: Array<string>;
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;

    token: string;
    description: string;
  };
};

export type RefreshTokenResult = {
  success: boolean;
  data: {
    /** `token` */
    accessToken: string;
    /** 用于调用刷新`accessToken`的接口时所需的`token` */
    refreshToken: string;
    /** `accessToken`的过期时间（格式'xxxx/xx/xx xx:xx:xx'） */
    expires: Date;
  };
};

export type DeviceTokenResult = {
  success: boolean;
  data: {
    id: number;
    main_device: string;
    main_device_ip: string;
    sub_device: string;
    sub_device_ip: string;
    main_device_token: string;
    sub_device_token: string;
  };
};

export type verifyCodeResult = {
  success: boolean;
  data: {
    /** `token` */
    verifyCodeUrl: string;
    verifyCode: string;
  };
};

/** 登录 */
export const getLogin = (data?: object) => {
  return http.request<UserResult>("post", "/api/user/login", { data });
};

/** 登录 */
export const getLogout = (data?: object) => {
  return http.request<UserResult>("get", "/api/user/logout", { data });
};

/** 刷新token */
export const refreshTokenApi = (data?: object) => {
  return http.request<RefreshTokenResult>("post", "/refreshToken", { data });
};

/** 获取验证码 */
export const getVerifyCodeApi = (data?: object) => {
  return http.request<verifyCodeResult>("get", "/api/user/getverifycode", {
    data,
  });
};

/** 更新token */
export const updateTokenApi = (data?: object) => {
  return http.request<RefreshTokenResult>(
    "post",
    "/api/device_relation/query",
    {
      data: {},
    }
  );
};

/** 更新token - 带参数 */
export const updateTokenWithParamsApi = (data: any) => {
  return http.request<DeviceTokenResult>(
    "post",
    "/api/device_relation/update",
    {
      data,
    }
  );
};
